/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.mods.shooter.entitysystem.systems;

import com.jme3.math.Vector3f;
import etherblocks.game.entitysystem.*;
import etherblocks.game.entitysystem.components.*;
import etherblocks.mods.shooter.entitysystem.components.*;

/**
 *
 * @author Carl
 */
public class RandomMoveSystem implements EntitySystem{

    @Override
    public void onNextFrameCalculation(EntityMap entityMap, float lastTimePerFrame){
        for(Entity entity : entityMap.getEntities(RandomMoveDirectionComponent.class)){
            float timeTillNextChange = (entity.getComponent(RandomMoveDirectionComponent.class).getTimeTillNextChange() - lastTimePerFrame);
            if(timeTillNextChange <= 0){
                float x = (float) (Math.random() - 0.5);
                float z = (float) (Math.random() - 0.5);
                Vector3f viewDirection = new Vector3f(x, 0, z).normalizeLocal();
                entity.setComponent(new RotationComponent(viewDirection, System.currentTimeMillis()));
                entity.setComponent(new MoveDirectionComponent(viewDirection.mult(0.03f)));
                timeTillNextChange = 3;
            }
            entity.setComponent(new RandomMoveDirectionComponent(timeTillNextChange));
        }
    }
}
